//
//  CommentComposeViewController.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/18.
//  Copyright © 2016年 albert. All rights reserved.
//  写点评

#import "CommentComposeViewController.h"
#import "NavBarView.h"
#import "JKTextView.h"

@interface CommentComposeViewController () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet JKTextView *textView;

/** textView底部约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomCons;

/** 自定义导航条 */
@property (nonatomic, weak) NavBarView *navBarView;
@end

@implementation CommentComposeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkin) name:JKSkinModelDidChangedNotification object:nil];
    [self updateSkin];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.textView resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.textView.delegate = self;
    self.navTitle = (self.navTitle == nil) ? @"评论" : [@"回复" stringByAppendingString:self.navTitle];
    NavBarView *navBarView = [NavBarView barViewWithTitle:self.navTitle showBackTitle:NO];
    [self.view addSubview:navBarView];
    self.navBarView = navBarView;
    
    UIButton *sendBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [sendBtn setTitle:@"发布" forState:(UIControlStateNormal)];
    [sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    navBarView.rightButton = sendBtn;
    sendBtn.x -= 6;
    
    __weak typeof(self) weakSelf = self;
    [navBarView setBackBtnClickBlock:^(UIButton *button) {
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)sendBtnClick{
    NSString *message = (!self.textView.hasText) ? @"请输入点评内容" : @"抱歉，其实您还未登录！";
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVc addAction:action];
    
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)keyboardWillChangeFrame:(NSNotification *)noti{
    // 1.获取键盘的frame
    CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算要移动的距离
    CGFloat offsetY = JKScreenH - rect.origin.y;
    self.bottomCons.constant = offsetY;
    
    // 3.获取动画当前节奏，如果节奏是7，连续执行两次动画会直接忽略上一个，进入下一个动画
    NSNumber *curve = noti.userInfo[UIKeyboardAnimationCurveUserInfoKey]; // 7
    
    // 4.执行动画
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView animateWithDuration:duration animations:^{
        [UIView setAnimationCurve:(curve.integerValue)];
        [self.view layoutIfNeeded];
    }];
}

#pragma mark - <UITextViewDelegate>
- (void)textViewDidChange:(UITextView *)textView{
    [self.textView textChange];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark - 更新皮肤
- (void)updateSkin{
    NSString *currentSkinModel = [[NSUserDefaults standardUserDefaults] objectForKey:JKCurrentSkinModelKey];
    
    if ([currentSkinModel isEqualToString:JKNightSkinModelValue]) { // 夜间模式
        self.view.backgroundColor = JKNightSkinColor;
        [self.navBarView changeToNightSkin];
        [self.textView changeToNightSkin];
        
    }else{ // 日间模式
        self.view.backgroundColor = JKGlobalBgColor;
        [self.navBarView changeToDaySkin];
        [self.textView changeToDaySkin];
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
